package com.github.data_manage.dao

import com.github.data_manage.domain.po.FundBusinessData
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
import java.math.BigDecimal
import java.time.LocalDate

/**
 *@author CHN
 *@since 2024-08-10 14:48
 */
interface FundBusinessDataDao : BaseDao<FundBusinessData> {

    fun findFundBusinessDataByFundIdAndTime(fundId: Int, time: LocalDate): FundBusinessData?

    @Query("select max(f.value) from FundBusinessData f where f.fundId = :fundId and f.time <= :time")
    fun findMaxValueByFundIdAndTime(@Param("fundId") fundId: Int, @Param("time") time: LocalDate): BigDecimal?

    fun deleteAllByFundId(fundId: Int)

    @Query("select f from FundBusinessData f where f.fundId = :fundId order by f.time")
    fun findAllByFundId(fundId: Int): List<FundBusinessData>

    fun findAllByTime(time: LocalDate): List<FundBusinessData>

    fun findAllByFundIdAndTimeLessThan(fundId: Int, time: LocalDate): List<FundBusinessData>

    fun findAllByFundIdAndTimeBetween(fundId: Int, startTime: LocalDate, endTime: LocalDate): List<FundBusinessData>

    @Query("select f from FundBusinessData f where f.fundId = :fundId and f.time >= :time")
    fun findLastOneYearData(@Param("fundId") fundId: Int, @Param("time") time: LocalDate): List<FundBusinessData>

    @Modifying
    @Query("update FundBusinessData f set f.fundName = :fundName where f.fundId = :fundId")
    fun updateFundNameByFundId(@Param("fundId") fundId: Int,
                               @Param("fundName") name: String)

    @Query("select max(f.time) from FundBusinessData f where f.fundId = :fundId")
    fun findMaxTimeByFundId(@Param("fundId")fundId: Int): LocalDate?

    @Query("select min(f.time) from FundBusinessData f where f.fundId = :fundId")
    fun findMinTimeByFundId(@Param("fundId")fundId: Int): LocalDate?

    @Query("select f from FundBusinessData f where f.fundId = :fundId order by f.time desc limit 1")
    fun findLastBusinessDataByFundId(@Param("fundId") fundId: Int): FundBusinessData?
}
